package com.zkingsoft.actions.dingding;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.model.Project;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.services.ProjectService;

/**
 * 钉钉机器人
 * 
 * @author admin
 *
 */
@Controller
@RequestMapping(value = "dd/robot")
public class DDRobotController {

	@Resource
	private ProjectService projectService;

	//public static String WEBHOOK_TOKEN = "https://oapi.dingtalk.com/robot/send?access_token=7ccec382d77e8cebc777a31582cfd0bdbb7012dbb914ee2c9fdad2e1c23f6ce7";
	public static String WEBHOOK_TOKEN =
	  "https://oapi.dingtalk.com/robot/send?access_token=7ccec382d77e8cebc777a31582cfd0bdbb7012dbb914ee2c9fdad2e1c23f6ce7";
	   
	// 项目信息格式
	private static String proMsg = "{\"msgtype\":\"markdown\"," + "\"markdown\":{\"title\":\"项目汇报-PRONAME \","
			+ "\"text\":\"# PRONAME-项目汇报 \n > ## 项目阶段：STATUS \n  ## 任务情况： 已完成YWC个，未完成WWC个\n  ## 项目周期：已开发YKF天，消耗YXH%的时长 \n  \"}}";

	
	/**
	 * 机器人发送项目情况
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/sendProjectStatus")
	public void sendProjectStatus() throws Exception {

		// 查询项目
		List<Project> proList = projectService.findByUserId(null, null, null);
		for (Project pro : proList) {
			if (!pro.getStatus().equals(Dictionary.PROJ_STATUS_ZSSX)
					&& !pro.getStatus().equals(Dictionary.PROJ_STATUS_XMSQ)) {
				sendProMsg(pro);
			}
		}

	}

	private static final String PRONAME = "PRONAME";// 项目名称
	private static final String STATUS = "STATUS";// 进行阶段
	private static final String YWC = "YWC";// 已完成任务
	private static final String WWC = "WWC";// 未完成任务
	private static final String YKF = "YKF";// 已开发天数
	private static final String YXH = "YXH";// 已消耗天数

	private static void sendProMsg(Project pro) {
		String msg = new String(proMsg);
		msg = msg.replaceAll(PRONAME, pro.getProjName());
		msg = msg.replaceAll(STATUS, pro.getStatus());
		Integer taskNum = pro.getTaskNum();
		Integer taskFinishNum = pro.getTaskFinishNum();
		Double projStatus = 0D;
		if (pro.getEndTime() != null) {
			projStatus =  ((double)(new Date().getTime() /(double)pro.getEndTime().getTime()) * 100);
		}
		DecimalFormat df = new DecimalFormat("######0.00");
		msg = msg.replaceAll(YWC, taskFinishNum == null ? "0" : taskFinishNum.toString());
		msg = msg.replaceAll(WWC, taskNum == null ? "0" : (taskNum - taskFinishNum) + "");
		msg = msg.replaceAll(YXH, df.format(projStatus));
		msg = msg.replaceAll(YKF, DateUtils.getDateInterval(pro.getCreateTime(), new Date()) + "");

		try {
			sendMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void sendMsg(String msg) throws Exception {
		HttpClient httpclient = HttpClients.createDefault();

		HttpPost httppost = new HttpPost(WEBHOOK_TOKEN);
		httppost.addHeader("Content-Type", "application/json; charset=utf-8");

		StringEntity se = new StringEntity(msg, "utf-8");
		httppost.setEntity(se);

		HttpResponse response = httpclient.execute(httppost);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			String result = EntityUtils.toString(response.getEntity());
			System.out.println(result);
		}
	}
}
